<template>
	<view class="viewBg">

		<map style="width: 100%; height: 750upx;" id="mapId" :latitude="location.latitude" :longitude="location.longitude"
		  :controls="mapControls" @tap="tapMap" @regionchange="regionChange" :scale="13">
		</map>
		<view class="address">滑动或缩放地图即可选择位置，确定即为当前标注位置</view>
		<button type="primary" @tap="selectLocation">确定</button>
	</view>
</template>

<script>
	import amap from "../../../common/amap-wx.js"
	export default {
		data() {
			return {
				location: {
					// longitude: 123.434,
					// latitude: 41.805,
				},
				address:'',
				mapControls: [{
					id: 2,
					position: {
						left: 160,
						top: 160,
						width: 30,
						height: 35
					},
					iconPath: '../../../static/img/location.png'
				}],
				screenWidth: 414,
				
				amapPlugin: null,
				mapKey: "fbb5ddc12d6183eba23f27639f49757a"
			};
		},
		onLoad(option) {
			var that = this;
			if(option.latitude){
				that.location = {latitude:option.latitude,longitude:option.longitude};
				console.log('之前选择的位置'+ JSON.stringify(that.location));
			}else{
				console.log('选择的位置');
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						console.log('xxxxxxxxxxx:', res.longitude +  res.latitude);
						that.location = {
							longitude: res.longitude,
							latitude: res.latitude
						};
					},
					fail: function(error) {
						console.log('获取位置错误2' + JSON.stringify(error));
					}
				})
			}
			

			uni.getSystemInfo({
				success: function(data) {
					var height = data.screenHeight;
					var width = data.screenWidth;
					that.screenWidth = data.screenWidth;
					that.mapControls[0].position.left = width / 2;
					that.mapControls[0].position.top = width / 2;
					console.log('屏幕宽度' + width + "ScreenWidth" + JSON.stringify(that.mapControls[0]));
				}
			})
		},
		methods: {
			regionChange(e) {
				console.log('视野变化');
			},
			tapMap(e) {
				console.log('点击地图' + JSON.stringify(e));
			},
			selectLocation() {
				console.log('视野移动');
				var self = this;
				var mapContext = uni.createMapContext("mapId", this);
				mapContext.getCenterLocation({
					success(res) {
						console.log('获取中心点' + JSON.stringify(res));
                        let point = new plus.maps.Point(res.longitude, res.latitude);
                        plus.maps.Map.reverseGeocode(
                            point,
                            {},
                            function(event) {
                                let address = event.address; // 转换后的地理位置
                                let point = event.coord; // 转换后的坐标信息
                                let coordType = event.coordType; // 转换后的坐标系类型
                                let reg = /.+?(省|市|自治区|自治州|县|区)/g;
	                            let addressList = address
		                            .match(reg)
		                            .toString()
		                            .split(',');
	                            console.log(JSON.stringify(addressList))
	                            res.address = address;
	                            self.$eventHub.$emit('fire', res);
	                            uni.navigateBack({})
                            },
                            function(e) {}
                        );
					}
				})
			}
		}
	}
</script>

<style>
	.viewBg {
		width: 100%;
		height: 100%;

		/* background-color: #0FAEFF; */
	}

	.address {
		margin: 30upx;
	}

	map {
		width: 100%;
		height: 750upx;
	}

	button {
		/* margin-top: 30upx; */
		/* margin-bottom: 30upx; */
		margin: 30upx;
	}
</style>
